<?php

class RM_eWAYpayments_Service {

    var $myGatewayURL;
    var $myCustomerID;
    var $myTransactionData = array();
    var $myCurlPreferences = array();


    //Payment Function
    function doPayment($customerID, $testmode = false, $preauthmode = false, $useStoredPaymentsURL = false) {

        if (!isset($customerID)) return false;

        $this->myCustomerID = $customerID;

        if ($useStoredPaymentsURL){

            $this->myGatewayURL = 'https://www.eway.com.au/gateway/xmlstored.asp';

        } else {

            if ($preauthmode || $preauthmode == "1"){

                // there is no test url for pre-auth payments
                $this->myGatewayURL = 'https://www.eway.com.au/gateway_cvn/xmlauth.asp';

            } else {

                if ($testmode || $testmode == "1"){
                    $this->myGatewayURL = 'https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp';
                } else {
                    $this->myGatewayURL = 'https://www.eway.com.au/gateway_cvn/xmlpayment.asp';
                }

            }
        }

        RM_Log::toLog("eWay Payments: payment submission URL: ".$this->myGatewayURL);

        $xmlRequest = "<ewaygateway><ewayCustomerID>" . $this->myCustomerID . "</ewayCustomerID>";
        foreach ($this->myTransactionData as $key => $value)
            $xmlRequest .= "<$key>$value</$key>";
        $xmlRequest .= "</ewaygateway>";

        $xmlResponse = $this->sendTransactionToEway($xmlRequest);

        if ($xmlResponse != "") {
            $responseFields = $this->parseResponse($xmlResponse);
            return $responseFields;
        }
        else
            die("Error in XML response from eWAY: " + $xmlResponse);
    }

    //Send XML Transaction Data and receive XML response
    function sendTransactionToEway($xmlRequest) {
        $x = $this;

        $ch = curl_init($this->myGatewayURL);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        foreach ($this->myCurlPreferences as $key => $value)
            curl_setopt($ch, $key, $value);

        $xmlResponse = curl_exec($ch);

        if (curl_errno($ch) == CURLE_OK)
            return $xmlResponse;
    }

    //Parse XML response from eway and place them into an array
    function parseResponse($xmlResponse) {
        $xml_parser = xml_parser_create();
        xml_parse_into_struct($xml_parser, $xmlResponse, $xmlData, $index);
        $responseFields = array();
        foreach ($xmlData as $data){
            if ($data["level"] == 2){
                $responseFields[$data["tag"]] = $data["value"];
            }
        }
        return $responseFields;
    }

    //Set Transaction Data
    //Possible fields: "TotalAmount", "CustomerFirstName", "CustomerLastName", "CustomerEmail", "CustomerAddress", "CustomerPostcode", "CustomerInvoiceDescription", "CustomerInvoiceRef",
    //"CardHoldersName", "CardNumber", "CardExpiryMonth", "CardExpiryYear", "TrxnNumber", "Option1", "Option2", "Option3", "CVN", "CustomerIPAddress", "CustomerBillingCountry"
    function setTransactionData($field, $value) {
        //if($field=="TotalAmount")
        //	$value = round($value*100);
        $this->myTransactionData["eway" . $field] = htmlentities(trim($value));
    }

    //receive special preferences for Curl
    function setCurlPreferences($field, $value) {
        $this->myCurlPreferences[$field] = $value;
    }

    //obtain visitor IP even if is under a proxy
    function getVisitorIP() {
        if(isset($_SERVER["REMOTE_ADDR"]) ) {
            $ip = $_SERVER["REMOTE_ADDR"];
        } else if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if(isset($_SERVER["HTTP_CLIENT_IP"]) ) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        } else $ip = "0.0.0.0";

        return $ip;
    }

}

?>